package cn.anline.www.controller;

import cn.anline.www.bean.UserBean;
import cn.anline.www.dao.UserDao;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.*;
import java.util.*;
import java.util.concurrent.Executor;

import static java.lang.System.*;

@Controller
@RequestMapping(value = "/user")
public class UserController {
    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    UserController(){
        out.println(getClass().toString()+"构造方法");
    }
    @RequestMapping("/")
    public Object _init(){
        out.println(getClass().toString()+"_init方法");
        return this.index();
    }
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public ModelAndView index(){
        out.println(getClass().toString()+"index方法");
        Map map = new HashMap();
        map.put("title","用户首页");
        map.put("nickname","Jiankian");
        return new ModelAndView("user/index",map);
    }

    @RequestMapping(value = "all",method = RequestMethod.GET)
    public ModelAndView all(){
        Collection<UserBean> collection = userDao.doquery();
        out.println("数据库查询结果");
        out.println(collection.toString());
        out.println("大小"+collection.size());
        out.println("大小2"+collection.toArray().length);
        for (int i=0;i<collection.size();i++){
            out.println("循环取值："+((UserBean)(collection.toArray()[i])).getId());
            out.println("循环取值："+((UserBean)(collection.toArray()[i])).getNickname());
            out.println("循环取值："+((UserBean)(collection.toArray()[i])).getPassword());
        }

        return new ModelAndView("user/all");
    }

    @RequestMapping(value = "add",method = RequestMethod.GET)
    public ModelAndView add(ModelMap modelMap,HttpServletRequest httpServletRequest,HttpSession httpSession){
        modelMap.addAttribute("get_page",httpServletRequest.getParameter("page"));
        return new ModelAndView("user/addUser",modelMap);
    }

    @RequestMapping(value = "add",method = RequestMethod.POST)
    public ModelAndView addUser(@ModelAttribute("springweb")UserBean userBean, ModelMap modelMap, HttpServletRequest httpServletRequest, HttpSession httpSession){
        modelMap.addAttribute("nickname", userBean.getNickname());
        modelMap.addAttribute("password", userBean.getPassword());
        modelMap.addAttribute("avatar", userBean.getAvatar());
        modelMap.addAttribute("get_id",httpServletRequest.getAttribute("id"));
        modelMap.addAttribute("get_page",httpServletRequest.getAttribute("page"));
        httpSession.setAttribute("userName","JIANKIAN");
        httpSession.setAttribute("userId",221240);
        httpSession.setAttribute("userToken", Base64.getUrlEncoder());

        return new ModelAndView("user/result",modelMap);
    }

    @RequestMapping(value = "/list")
    public ModelAndView list(ModelMap modelMap){

        return new ModelAndView("user/list",modelMap);
    }



}
